﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Gui.Net
{
    class DuplicateChecker
    {
        private string name_;
        private const int DEFAULT_WAIT_TIMEOUT = 1000;
        private Mutex mutex_;
        
        public DuplicateChecker( string name )
        {
            name_ = name;
            mutex_ = new Mutex(false, name_);
        }

        public bool Check()
        {
            bool returnValue = false;
            try
            {
                returnValue = mutex_.WaitOne(DEFAULT_WAIT_TIMEOUT, false);
            }
            catch (AbandonedMutexException /*ex*/)
            {
                returnValue = true;
            }
            catch (ObjectDisposedException /*ex*/)
            {
                // TODO: log error            	
            }
            catch (ArgumentOutOfRangeException /*ex*/)
            {
                // TODO: log error
            }
            catch (InvalidOperationException /*ex*/)
            {
                // TODO: log error
            }


            return returnValue;
        }
    }
}
